package cn.yangke.org.service.impl;

import cn.yangke.basic.exception.BusinessException;
import cn.yangke.basic.service.impl.BaseServiceImpl;
import cn.yangke.basic.util.BaiduAuditUtils;
import cn.yangke.basic.util.MailUtil;
import cn.yangke.org.domain.Employee;
import cn.yangke.org.domain.Shop;
import cn.yangke.org.domain.ShopAuditLog;
import cn.yangke.org.mapper.EmployeeMapper;
import cn.yangke.org.mapper.ShopAuditLogMapper;
import cn.yangke.org.mapper.ShopMapper;
import cn.yangke.org.service.IShopService;
import cn.yangke.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yangke
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        //进行后端的非空判断
        if(StringUtils.isEmpty(shop.getName())
            || StringUtils.isEmpty(shop.getAddress())
            || StringUtils.isEmpty(shop.getTel())
            || StringUtils.isEmpty(admin.getUsername())
            || StringUtils.isEmpty(admin.getEmail())
            || StringUtils.isEmpty(admin.getPassword())
            || StringUtils.isEmpty(admin.getPhone())
        ){
            throw new BusinessException("输入的信息不能为空!!");
        }

        /**
         * 百度ai审核
         */
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("非法字符,请重新输入!!");
        }

        //判断该店铺名是否存在了
        Shop shopTmp = shopMapper.loadByName(shop.getName());
        //shopTmp不为null说明该店名已经被注册
        if(shopTmp != null){
            throw new BusinessException("该店铺名已被注册,请直接登录!若忘记密码,请请改密码!");
        }
        //判断用户名是否存在
        Employee empTmp = employeeMapper.loadByUsername(shop.getAdmin().getUsername());
        //empTmp不为null说明该用户名已被注册,并且名下以有店铺
        if(empTmp != null && empTmp.getShop_id() != null){
            throw new BusinessException("该用户名下已存在运营中的店铺,请更换其他用户!");
        }
        //如果empTmp为null说明该用户未注册
        if(empTmp == null){
            //新增该用户至员工表
            employeeMapper.save(admin);
            empTmp = admin;
        }

        //保存数据
        //设置adminid
        shop.setAdminId(empTmp.getId());
        //新增用户
        shopMapper.save(shop);
        //修改employee中的shop_id
        empTmp.setShop_id(shop.getId());
        employeeMapper.update(empTmp);
    }

    @Override
    @Transactional
    public void reject(ShopAuditLog log) {
        //修改店铺状态为 2驳回
        Shop shop = shopMapper.loadById(log.getShopId());
        //修改状态
        shop.setState(2);
        //调用修改方法
        shopMapper.update(shop);
        //设置审核人
        log.setAuditId(324L);
        //新增驳回的审核记录
        shopAuditLogMapper.save(log);
        Employee employee = employeeMapper.loadByShopId(log.getShopId());

        try {
            MailUtil.complexMail(javaMailSender
                    ,"510014607@qq.com"
                    ,employee.getEmail()
                    ,"宠物乐园-店铺入驻驳回任务"
                    ,"亲，您的店铺信息有误，审核不通过，请尽快修改并重新提交审核！！修改链接：http://localhost:8081/#/register?shopId=" + log.getShopId()
                    ,true
                    ,null,null);
        } catch (Exception e) {
            e.printStackTrace();
        }


        /*SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("510014607@qq.com");
        message.setTo(employee.getEmail());
        message.setSubject("宠物乐园-店铺入驻驳回任务");
        message.setText("亲，您的店铺信息有误，审核不通过，请尽快修改并重新提交审核！！修改链接：" +
                "http://localhost:8081/#/register2?shopId=" + log.getShopId());
        javaMailSender.send(message);*/
    }

    /**
     * 审核通过
     * @param log
     */
    @Override
    @Transactional
    public void pass(ShopAuditLog log) {
        //更改店铺状态为通过
        Shop shop = shopMapper.loadById(log.getShopId());
        //修改状态
        shop.setState(3);
        //调用修改方法
        shopMapper.update(shop);
        //设置审核人
        log.setAuditId(324L);
        //新增通过的审核记录
        shopAuditLogMapper.save(log);
        //审核通过之后给用户发送激活连接
        Employee employee = employeeMapper.loadByShopId(log.getShopId());

        try {
            MailUtil.complexMail(javaMailSender
                    ,"510014607@qq.com"
                    ,employee.getEmail()
                    ,"宠物乐园-店铺入驻激活请求"
                    ,"亲，您的店铺信息已经通过审核，请尽快完成激活并使用：" +
                            "激活链接：http://localhost:8080/shop/activation/"+log.getShopId()
                    ,true
                    ,null,null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        /*SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("510014607@qq.com");
        message.setTo(employee.getEmail());
        message.setSubject("宠物乐园-店铺入驻激活请求");
        message.setText("亲，您的店铺信息已经通过审核，请尽快完成激活并使用：" +
                "激活链接：http://localhost:8080/shop/activation/"+log.getShopId());
        javaMailSender.send(message);*/



    }

    /**
     * 激活店铺
     * @param shopId 店铺id
     */
    @Override
    @Transactional
    public void activation(Long shopId) {
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(1);
        shopMapper.update(shop);
    }

    /**
     * 查询前端报表
     * @return
     */
    @Override
    public List<ShopVo> queryEcharts() {

        return shopMapper.queryEcharts();
    }

    /**
     * 批量新增
     * @param shops
     */
    @Override
    public void batchSave(List<Shop> shops) {
        shopMapper.batchSave(shops);
    }
}
